package com.iotteam.androidbase.moudules.sensor_history.presenter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.BaseApplication;
import com.iotteam.androidbase.commen.LocalInfoManager;
import com.iotteam.androidbase.commen.network.Api;
import com.iotteam.androidbase.commen.network.core.HttpListener;
import com.iotteam.androidbase.commen.network.core.HttpManager;
import com.iotteam.androidbase.commen.utils.PLog;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.sensor_history.model.HistoryModel;
import com.iotteam.androidbase.moudules.sensor_history.view.ISensorHistoryInfoView;
import com.iotteam.androidbase.moudules.sensor_history.view.SensorHistoryInfoActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lidechen on 6/25/17.
 */

public class SensorHistoryInfoPresenter implements ISensorHistoryInfoPresenter {

    private static final String TAG = "SensorHistoryInfoPresenter";

    private ISensorHistoryInfoView mView;

    private List<HistoryModel.DataBean> mHistoryInfoList;
    private HistoryInfoItemAdapter mHistoryInfoItemAdapter;

    public SensorHistoryInfoPresenter(ISensorHistoryInfoView view){
        mView = view;
    }

    @Override
    public void getSensorHistoryInfo() {

        Map<String, String> param = new HashMap<>();
        param.put("mobile", LocalInfoManager.getInstance().getUserInfo().getMobile());

        param.put("terminalid", "861694034305647");
        param.put("password", "bb0a39");
        param.put("begints", "2017-06-02");
        param.put("stopts", "2017-08-08");

        HttpManager.getInstance().postBean(Api.getHistoryInfo, param, HistoryModel.class, new HttpListener<HistoryModel>() {
            @Override
            public void onSucceed(int what, HistoryModel bean) {
                if(bean.getCode() == 0){
                    PLog.i(TAG, bean.toString());

                    mHistoryInfoList = bean.getData();
                    mHistoryInfoItemAdapter = new HistoryInfoItemAdapter(mHistoryInfoList);
                    mView.setAdapter(mHistoryInfoItemAdapter);

                }else{
                    if(!TextUtils.isEmpty(bean.getMessage())) {
                        ToastUtil.showShort(bean.getMessage());
                    }else{
                        ToastUtil.showShort(R.string.dialog_reuest_info_err);
                    }
                }
            }

            @Override
            public void onFailed(int what, Exception e) {
                ToastUtil.showShort(R.string.dialog_reuest_info_err);
            }

            @Override
            public void onStart(int what) {
                mView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                mView.dismissLoading();
            }
        });
    }

    public static class HistoryInfoItemAdapter extends RecyclerView.Adapter<SensorHistoryInfoActivity.HistoryInfoItemHolder>{

        List<HistoryModel.DataBean> mList;

        public HistoryInfoItemAdapter(List<HistoryModel.DataBean> list){
            mList = list;
        }

        @Override
        public SensorHistoryInfoActivity.HistoryInfoItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            SensorHistoryInfoActivity.HistoryInfoItemHolder holder = new SensorHistoryInfoActivity.HistoryInfoItemHolder(
                    LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.layout_history_info_item, parent, false));

            return holder;
        }

        @Override
        public void onBindViewHolder(SensorHistoryInfoActivity.HistoryInfoItemHolder holder, int position) {

            HistoryModel.DataBean bean = mList.get(position);

            holder.terminalidText.setText("terminalid:"+bean.getTerminalid());
            holder.tsText.setText("ts:"+bean.getTs());
            holder.posxText.setText("posx:"+bean.getPosx());
            holder.posyText.setText("posy:"+bean.getPosy());
            holder.anglesText.setText("angles"+bean.getAngles());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }
}







